<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/3/28
 * Time: 17:17
 */

namespace common\models\datas;


use common\components\bases\BaseModel;
use common\components\extenders\MenuManage;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

class MediasPosition extends BaseModel
{
    public static function tableName(){
        return '{{%medias_position}}';
    }

    public function rules()
    {
        return [
            [['created_at','updated_at'], 'integer'],
            [['posrelated_route', 'mediaposition_name'], 'string', 'max' => 255],
            [['posrelated_route','appid_name'], 'required'],
        ];
    }

    public function attributeLabels()
    {
        return [
            'id' => \Yii::t('common', '自动编号'),
            'mediaposition_name' => \Yii::t('common', '媒体位置名称'),
            'posrelated_route' => \Yii::t('common', '位置相关路由'),
            'appid_name' => \Yii::t('common','应用id'),
            'created_at' => \Yii::t('common', '创建时间'),
            'updated_at' => \Yii::t('common', '修改时间'),
            'is_delete' => \Yii::t('common','是否删除')
        ];
    }

    public function fields(){

        $fields = parent::fields();
        $fields['updated_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->updated_at);
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        $fields['is_delete'] = function () {
            switch ($this->is_delete) {
                case 0:
                    return \Yii::t('common', '正常');
                case 1:
                    return \Yii::t('common', '删除');
            }
        };
        return $fields;
    }

    public function extraFields(){
        return [
            'backend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
        ]);
    }
}